// pages/coupon/index.js
var app = getApp()

Page({
  data: {
    openid:'',
    page:1,
    res:[],//分页数据
    nomore: false,
    list:[]
  },

  onLoad: function (options) {
    this.checkOpenid();
    //this.getList();
  },

  onShow: function () {
    var openid = this.data.openid;
    if(openid != ''){
      this.setData({
        list: [],
        nomore: false,
        page: 1
      });
      this.getList();
    }
  },

  checkOpenid: function(){
    var openid = wx.getStorageSync('userOpenid');
    if(openid != ''){
      this.setData({
        openid: openid
      })
    }else{
      console.log('getopenid');
      this.getUserOpenid();
    }
  },

  getUserOpenid: function(){
    var that=this;
    wx.login({
      success: function (res) {
        if (res.code) {
          app.util.request({
            url: 'entry/wxapp/getUserOpenid',
            data: {
              code: res.code
            },
            cachetime: 30,
            success: function (res) {
              var openid = res.data.data.openid;
              wx.setStorageSync('userOpenid', openid);
              that.setData({
                openid: openid
              });
              that.getList()
            }
          })
        }
      }
    });
  },

  getList: function(){
    var that = this;
    app.util.request({
      url: 'entry/wxapp/getMyCouponList',
      data: {
         openid:that.data.openid,
         page: that.data.page
      },
      cachetime: 0,
      success: function (res) {
        if(res.data.errno == 0){
          that.setData({
            res: res.data.data.list
          });
          var page = that.data.page;
          var list = that.data.list;
          var res = that.data.res;
          if(res.length>0){
            that.setData({
              list: list.concat(res),
              page: page + 1,
            })
          }else if(that.data.list.length>0){
            that.setData({
              nomore: true,
              res:[]
            })
          }
          //隐藏loading 提示框
          wx.hideLoading();
          //隐藏导航条加载动画
          wx.hideNavigationBarLoading();
          //停止下拉刷新
          wx.stopPullDownRefresh();
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        });
        return false;
      }
    })
  },

  code: function(e){
    var recordid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'code?recordid=' + recordid,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      nomore: false,
      page: 1
    })
    this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})